/**
 * @author Sergio
 * Description: 
 */

$(document).bind("mobileinit", function(){
      $.mobile.defaultPageTransition = 'none';
      $.mobile.defaultDialogTransition = 'none';
      $.mobile.useFastClick = true;
      $.mobile.pushStateEnabled = false;
});

function renameDroitUser(droit) {
	var userDroit = "";
	switch (droit) {
		case "superuser":
			userDroit = "(Super Manager)";
			break;
		case "readwrite":
			userDroit = "(Manager)";
			break;
		case "readonly":
			userDroit = "(Viewer)";
			break;
		default:
			userDroit = "(Root)";
	}
	return userDroit;
}

// Action pour l'authentification
$('#connexionButton').bind('click singletap',function(event){
		var membreMainPage = $('#main-page');
		var login = $('#login').val();
		var password = $('#password').val();
		password = window.btoa(password);
	    var data = {
	        "action": "connexion",
	        "login": login,
	        "password": password
	    };
	    data = $(this).serialize() + "&" + $.param(data);
		$("#loginForm").empty();
		$("#loginForm").css({"background-image":"none"});
		$("#loginForm").css({"background":"none"});
        $("#imgLoginWaiting").css({"display": "inline"});
        
        if (!login) {
			$("#alertLoginDiv").css({"display": "inline"});
			msg = "Veuillez compléter votre login. ";
			document.getElementById("alertLogin").innerHTML = msg;
			$("#imgLoginWaiting").css({"display": "none"});
			$(".backToLoginDiv").css({"display": "inline"});
        }else if (!password) {
        	$("#alertLoginDiv").css({"display": "inline"});
			msg = "Veuillez compléter votre mot de passe. ";
			document.getElementById("alertLogin").innerHTML = msg;
			$("#imgLoginWaiting").css({"display": "none"});
			$(".backToLoginDiv").css({"display": "inline"});
        }else if (!password && !login) {
        	$("#alertLoginDiv").css({"display": "inline"});
			msg = "Veuillez remplir les champs login et mot de passe";
			document.getElementById("alertLogin").innerHTML = msg;
			$("#imgLoginWaiting").css({"display": "none"});
			$(".backToLoginDiv").css({"display": "inline"});
        }else {
		    $.ajax({
		        type: 'POST',
		        url: 'https://tacks.fr/application/action.php',
		        dataType: 'jsonp',
		        jsonp: 'jsoncallback',
		        timeout: 5000,
		        data: data,
		        crossDomain: true,
		        success: function(data, status){
		        	console.log('Test connexion');
		        	console.log(data);
		        	if (data['status'] == 0) {
		        		console.log(data['msg']);
						
						$("#alertLoginDiv").css({"display": "inline"});
		        		var msg = "Login ou mot de passe incorrect.";
						$("#alertLogin").empty();
						document.getElementById("alertLogin").innerHTML = msg;
		        		$("#imgLoginWaiting").css({"display": "none"});
						$(".backToLoginDiv").css({"display": "inline"});
						
		        	}else if(data['status'] == 1) {
		        		console.log(data['msg']);
		        		for (var key in data){
		        			if (key != "status" && key != "msg") {
		        				 var value = data[key]; 
		        				 window.localStorage.setItem(key, value);
		        			}
		        		}
		        		$("#imgLoginWaiting").css({"display": "none"});
		        		window.location.replace("../view/membre.php");
		        	}
		        },
		        error: function(data,jqXHR,xhr, desc, err) {
		            console.log(data);
		            console.log(xhr);     
		            console.log("Details: " + desc + "\nError:" + err);  
		            console.warn('Connection failed');
					$("#alertLoginDiv").css({"display": "inline"});
					msg = "Serveur inaccessible temporairement, veuillez réessayer plus tard. ";
					document.getElementById("alertLogin").innerHTML = msg;
					$("#imgLoginWaiting").css({"display": "none"});
					$(".backToLoginDiv").css({"display": "inline"});
		        }
		    });
        }
});


// MENU - Ajouter une list
$('#bnt-menu').bind('click singletap',function(event){
	var login = window.localStorage.getItem("login");
	var droit = window.localStorage.getItem("droit");
	var renameDroit = renameDroitUser(droit);
	document.getElementById("userLoged").innerHTML = login;
	document.getElementById("userDroit").innerHTML = renameDroit;
	
	
	// Affichage du ménu en fonctin du doit user
	if (droit == "readonly"){
		$("#btn-gestionUsers").css({"display": "none"});
	}else if(droit == "readwrite") {
		$("#btn-gestionUsers").css({"display": "none"});
	}else {
		$("#btn-gestionUsers").css({"display": "inline"});
	}
});

// DECONNEXION
$('#deconnexion').bind('click singletap',function(event){
        window.localStorage.clear();
        window.location.replace("../view/index.php");
});

$('#forgetPassword').bind('click singletap',function(event){
	$('#loginForm').css({"display": "none"});
	$("#forgetForm").css({"display": "inline"})
});


$('.backToLoginDiv').bind('click singletap',function(event){
	window.location.replace("../view/index.php");
});

$('#valideEmalForget').bind('click singletap',function(event){
	
	var emailA = $('#emailForget').val();
	email = window.btoa(emailA);
	var conf_email = $('#conf_emailForget').val();
	confirm_email = window.btoa(conf_email);
	
	if(!email && !confirm_email) {
		$("#paraInfoResetPwd").css({"display": "inline"});
		$("#iMsgPwd").empty();
		msg = " Veuillez rentrer votre adresses email suivis de la confirmation. ";
		document.getElementById("iMsgPwd").innerHTML = msg;
		
	}else if(!email) {
		$("#paraInfoResetPwd").css({"display": "inline"});
		$("#iMsgPwd").empty();
		msg = " Veuillez compléter votre adresse email.";
		document.getElementById("iMsgPwd").innerHTML = msg;
		
	}else if(!confirm_email) {
		$("#paraInfoResetPwd").css({"display": "inline"});
		$("#iMsgPwd").empty();
		msg = " Veuillez confirmer votre adresse email.";
		document.getElementById("iMsgPwd").innerHTML = msg;
		
	}else if(email != confirm_email) {
		$("#paraInfoResetPwd").css({"display": "inline"});
		$("#iMsgPwd").empty();
		msg = " Vos adresses email ne sont pas identiques, veuillez recommencer.";
		document.getElementById("iMsgPwd").innerHTML = msg;
		
	}else {
		console.log(email + " " + confirm_email);
	    var data = {
	        "action": "resetPassword",
	        "email": email,
	        "confirm_email": confirm_email
	    };
	     
	    data = $(this).serialize() + "&" + $.param(data); 
	    $.ajax({
	        type: 'POST',
	        url: 'https://tacks.fr/application/action.php',
	        dataType: 'jsonp',
	        jsonp: 'jsoncallback',
	        timeout: 5000,
	        data: data,
	        crossDomain: true,
	        success: function(data, status){
	    		console.log('Test reset password');
	        	console.log(data);
	        	if(data['status'] == 1) {
	        		console.log(data['msg']);
					$("#firstDivResetPwd").css({"display": "none"});
					$("#secondDivResetPwd").css({"display": "inline"});
	        	}
	        },
	        error: function(data,jqXHR,xhr, desc, err) {
	            console.log(data);
	            console.log(xhr); 
	            console.log(jqXHR);
	            console.log("Details: " + desc + "\nError:" + err);  
	            console.warn('Connection failed');
				$("#firstDivResetPwd").css({"display": "none"});
				$("#secondDivResetPwd").css({"display": "inline"});
	        }
	    }); 
	}
});

// Function alert message HTML
function alertMsgHtml(idDiv, idParagraph, msg) {
	$("#"+idDiv).css({"display": "inline"});
	$("#"+idParagraph).append(msg);
	$("#"+idParagraph).trigger("create");
}

